from concurrent.futures import ThreadPoolExecutor

from flask import Flask
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from app.Vendor.Code import Code
from app.env import SQLALCHEMY_DATABASE_URI, SQLALCHEMY_TRACK_MODIFICATIONS, UPLOAD_FOLDER, MAX_CONTENT_LENGTH

app = Flask(__name__)
# 配置 sqlalchemy  数据库驱动://数据库用户名:密码@主机地址:端口/数据库?编码
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = SQLALCHEMY_TRACK_MODIFICATIONS
# 上传文件配置
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER  # 上传目录
app.config['MAX_CONTENT_LENGTH'] = MAX_CONTENT_LENGTH  # 上传大小
# 创建数据库及连接
engine = create_engine(SQLALCHEMY_DATABASE_URI)
# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)
dBSession = DBSession()

device_run_status = '运行'
device_status = 'OK'
current_vin = ''
executor = ThreadPoolExecutor(max_workers=1)
# # 引入使用的控制器
from app.Vendor.ExceptionApi import ExceptionApi
from app.Controllers.LotController import lot
from app.Controllers.DoorVehicleController import doorVehicle
from app.Controllers.FloorVehicleController import floorVehicle

app.register_blueprint(lot, url_prefix='/lot')
app.register_blueprint(doorVehicle, url_prefix='/vehicle')
app.register_blueprint(floorVehicle, url_prefix='/floorVehicle')


@app.teardown_appcontext
def shutdown_session(exception=None):
    dBSession.close()


# 挂载500异常处理,并记录日志
@app.errorhandler(Exception)
def error_handler(e):
    return ExceptionApi(Code.ERROR, e)
